import threading
from datetime import datetime
import time
import redis
from Ini_utils import UtilIni


# 生成订单编号
def make_order_sn():
    order_sn = ((datetime.now().strftime('%Y%m%d%H')) + str(int(time.time())))
    UtilIni().writeln("CHARGER", "ordersn", order_sn)
    return order_sn


def getChargeOrderSN():
    return datetime.now().strftime('%Y%m%d%H') + str(round(time.time()) * 1000)


def get_charge_time(s):
    now_date = datetime.now().strftime("%Y-%m-%d")
    str = f"{now_date} {UtilIni().getvalue('CHARGER', s)}"
    return str


def get_spot_time(s):
    now_date = datetime.now().strftime("%Y-%m-%d")
    str = f"{now_date} {UtilIni().getvalue('CHARGE_SPOT', s)}"
    return str


# 多线程启动
def threadSend(event):
    """
    多线程处理
    :param event: 程序
    :return:
    """
    t = threading.Thread(event)
    t.setDaemon(True)
    t.start()


# 连接redis库
def redis_client(host, port, pwd):
    pool = redis.ConnectionPool(host=host, port=port, password=pwd,
                                decode_responses=True, db=2)
    redis_conn = redis.Redis(connection_pool=pool)
    return redis_conn

    # 读取excel文件


def read_excel_value(filename, index):
    workbook = xlrd.open_workbook(filename)
    sheet = workbook.sheet_by_index(index)
    params_data = []
    for i in range(1, sheet.nrows):
        params_data.append(tuple(sheet.row_values(i)))
    return params_data


# 绑定事件函数封装
def write_to_conf(sec, opt, value):
    return UtilIni().writeln(section=sec, option=opt, valuetext=value)


# 获取当前日期
def get_current_date():
    current_date = time.strftime('%Y-%m-%d', time.localtime())
    return current_date


# 获取当前时间戳
def get_current_time():
    current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    return current_time


# 将字符串转换为字典格式
def str_to_dict(input_str):
    try:
        # 使用 eval() 函数将字符串解析为字典
        result_dict = eval(input_str)
        if isinstance(result_dict, dict):
            return result_dict
        else:
            raise ValueError("Input is not in dictionary format")
    except Exception as e:
        print("Error occurred while converting string to dictionary:", e)
        return {}


def get_nested_value(input_dict, keys):
    current_dict = input_dict
    for key in keys:
        if isinstance(current_dict, dict) and key in current_dict:
            current_dict = current_dict[key]
        else:
            return None
    return current_dict


import json

def extract_value_from_json_file(file_path):
    try:
        with open(file_path, 'r',encoding='utf-8') as file:
            data = json.load(file)
            return data
    except Exception as e:
        print("Error occurred while extracting value from JSON list:", e)
        return None

# 示例用法
